package com.gopay.bis.manager.bank.expay;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.dao.bankorder.BankContractInfoDAO;
import com.gopay.bis.manager.bank.CustCorpInfoManager;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.cps.manager.order.CpsGeneralMainOrderQueryManager;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.ceb.expay.CebExPayMessage;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageNSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageNSQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessagePPReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageQPReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageQSCReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageQUSReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExPayMessageUSCReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpayNSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpayNSQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpayPPReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpayQSCReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpayQUSReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpaySuitNSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpaySuitNSQReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpaySuitPPReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpaySuitQSCReq;
import com.gopay.common.domain.bank.ceb.expay.req.CebExpaySuitQUSReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageQPReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageSuitQPReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageSuitSOQReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageSuitUSCReq;
import com.gopay.common.domain.bank.ceb.expay.req.plain.MessageUSCReq;
import com.gopay.common.domain.bank.ceb.expay.res.CebExPayMessagePPRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitNSOQRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitNSQRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitPPRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitQSCRes;
import com.gopay.common.domain.bank.ceb.expay.res.CebExpaySuitQUSRes;
import com.gopay.common.domain.bank.ceb.expay.res.msg.MessageSuitQPRes;
import com.gopay.common.domain.bank.ceb.expay.res.msg.MessageSuitSOQRes;
import com.gopay.common.domain.bank.ceb.expay.res.msg.MessageSuitUSCRes;
import com.gopay.common.domain.bis.BaseBisBankBizProd;
import com.gopay.common.domain.bis.BisBankContractInfo;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.CommonUtils;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.businesses.ExPayService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;

/**
 * 光大快捷支付报文拼装
 * 
 * @CebExPayManager.java
 * @author lilili
 * 
 */
public class CebExPayManager {

    @Resource(name = "cebExPayServiceClient")
    private ExPayService cebExPayServiceClient;
    @Resource(name = "cebConfig")
    private Map<String, String> cebConfig;
    
    @Resource(name = "custCorpInfoManager")
    CustCorpInfoManager custCorpInfoManager;

    @Resource(name = "cpsGeneralMainOrderQueryManager")
    private CpsGeneralMainOrderQueryManager mainOrderQuery;
    
    @Resource(name = "bankContractInfoDAO")
    private BankContractInfoDAO bankContractInfoDAO;

    private static final String DATE_FORMAT = "yyyyMMdd HH:mm:ss"; // 时间格式

    /**
     * 签约接口
     * 
     * @param message
     * @return
     * @throws GopayException
     */
    public Object certification(CebExPayMessage msg) throws GopayException {
        CebExPayMessage message = (CebExPayMessage) msg;
        CebExpaySuitQUSReq qusMes = this.buildSuitQUSReqPlainMsg(message);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CebExpaySuitQUSReq.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(qusMes, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        message.setPlainMessage(plainStr);
        BankLogUtils.printExPayManagerCertificationSendLog(message);
        CebExpaySuitQUSRes res = (CebExpaySuitQUSRes) cebExPayServiceClient.certification(message);
        res.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        BankLogUtils.printExPayManagerCertificationReturnLog(res);
        return res;
    }

    /**
     * 签约查询接口
     * 
     * @param msg
     * @return
     * @throws GopayException
     * @author w_jiqing
     */
    public Object queryCert(CebExPayMessage msg) throws GopayException {
        CebExPayMessage message = (CebExPayMessage) msg;
        CebExpaySuitNSQReq queryMes = this.buildSuitNSQReqPlainMsg(message);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CebExpaySuitNSQReq.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(queryMes, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        message.setPlainMessage(plainStr);
        BankLogUtils.printExPayManagerSignQuerySendLog(message);
        CebExpaySuitNSQRes res = (CebExpaySuitNSQRes) cebExPayServiceClient.signQuery(message);
        res.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        BankLogUtils.printExPayManagerSignQueryReturnLog(res);
        return res;

    }

    /**
     * 解约接口
     * 
     * @param msg
     * @return
     * @throws GopayException
     * @author w_jiqing
     */
    public Object UnBindCard(CebExPayMessage msg, String signNo) throws GopayException {
        CebExPayMessage message = (CebExPayMessage) msg;
        CebExpaySuitQSCReq messageSuit = this.buildSuitQSCReqPlainMsg(message, signNo);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CebExpaySuitQSCReq.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(messageSuit, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        message.setPlainMessage(plainStr);
        BankLogUtils.printExPayUnBindCardSendLog(message);
        CebExpaySuitQSCRes res = (CebExpaySuitQSCRes) cebExPayServiceClient.cancel(message);
        res.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        BankLogUtils.printExPayUnBindCardRetuenLog(res);
        return res;

    }

    /**
     * 支付请求
     * 
     * @param msg
     * @return
     * @throws GopayException
     */
    public Object payment(BaseMessage msg) throws GopayException {
        CebExPayMessage message = (CebExPayMessage) msg;
        CebExpaySuitPPReq messageSuit = this.buildPPReqPlainMsg(message);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CebExpaySuitPPReq.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(messageSuit, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        message.setPlainMessage(plainStr);
        BankLogUtils.printExPayPaymentSendLog(message);
        CebExpaySuitPPRes res = (CebExpaySuitPPRes) cebExPayServiceClient.payment(message);
        res.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        BankLogUtils.printExPayPaymentReturnLog(res,null);
        return res;
    }

    /**
     * 发送交易查询请求
     * 
     * @param baseMsg
     * @return
     * @throws GopayException
     */
    public Object query(BaseMessage baseMsg) throws GopayException {
        CebExPayMessage message = (CebExPayMessage) baseMsg;
        CebExpaySuitNSOQReq messageSuit = this.buildNSOQPlainMsg(message);
        XStream xs = new XStream();
        xs.autodetectAnnotations(true);
        xs.processAnnotations(CebExpaySuitNSOQReq.class);
        OutputStream output = new ByteArrayOutputStream();
        xs.marshal(messageSuit, new CompactWriter(new OutputStreamWriter(output)));
        String plainStr = output.toString();
        message.setPlainMessage(plainStr);
        BankLogUtils.printExPayManagerQuerySendLog(message);
        CebExpaySuitNSOQRes res = (CebExpaySuitNSOQRes) cebExPayServiceClient.queryOrder(message);
        res.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
        BankLogUtils.printExPayManagerQueryReturnLog(res);
        return res;
    }

    /**
     * 构建解约请求对象
     * 
     * @param message
     * @return
     * @author w_jiqing
     */
    public CebExpaySuitQSCReq buildSuitQSCReqPlainMsg(CebExPayMessage message, String signNo) {
        CebExpaySuitQSCReq suitQsc = new CebExpaySuitQSCReq();
        CebExpayQSCReq qsc = new CebExpayQSCReq();
        CebExPayMessageQSCReq qscMes = new CebExPayMessageQSCReq();
        // 交易代码
        qscMes.setTransId(cebConfig.get("CEB.EXPAY.QSCREQ.TRANSID"));
        // 商户号
        qscMes.setMerId(message.getMerID());
        // 交易日期和时间
        qscMes.setDate(DateUtils.format(new Date(), DATE_FORMAT));
        // 交易流水号
        qscMes.setTrxId(cebConfig.get("CEB.EXPAY.QSCREQ.TRANSID") + message.getOrderId());
        // 协议号
        qscMes.setSignNo(signNo);
        qsc.setPlain(qscMes);
        qsc.setId(this.getStringid());
        suitQsc.setMessage(qsc);
        message.setTransId(qscMes.getTransId());
        return suitQsc;

    }

    /**
     * 构建签约请求对象
     * 
     * @param message
     * @return
     * @author w_jiqing
     */
    public CebExpaySuitQUSReq buildSuitQUSReqPlainMsg(CebExPayMessage message) {
        CebExpaySuitQUSReq suitQus = new CebExpaySuitQUSReq();
        CebExpayQUSReq qus = new CebExpayQUSReq();
        CebExPayMessageQUSReq qusMes = new CebExPayMessageQUSReq();
        // 交易代码
        qusMes.setTransId(cebConfig.get("CEB.EXPAY.QUSREQ.TRANSID"));
        // 商户号
        qusMes.setMerId(message.getMerID());
        // 银行卡户名
        qusMes.setName(message.getCardHolder());
        // 证件类型 1-身份证
        qusMes.setCertType("1");
        // 证件号码
        qusMes.setCertNo(message.getIdentityCode());
        // 手机号码
        qusMes.setPhone(message.getMediaId());
        // 银行卡号
        qusMes.setCardNo(message.getCardNo());
        if (BisBankBusinessProd._05.value.equals(message.getBankBusProd().value)) {
            // 银行卡类型
            qusMes.setCardType("C");
            // 有效期
            qusMes.setValidDate(message.getAvailableDate());
            // cvv2
            qusMes.setCvv2(message.getCvv2());
        } else {
            // 银行卡类型
            qusMes.setCardType("D");
        }
        // 商户名称
        qusMes.setMrchntNm("国付宝信息科技有限公司");
        // 交易流水号
        qusMes.setTrxId(cebConfig.get("CEB.EXPAY.QUSREQ.TRANSID") + message.getOrderId());
        // 交易日期和时间
        qusMes.setDate(DateUtils.format(new Date(), DATE_FORMAT));
        qus.setPlain(qusMes);
        qus.setId(this.getStringid());
        suitQus.setMessagae(qus);
        message.setTransId(qusMes.getTransId());
        return suitQus;

    }

    /**
     * 构建签约查询对象
     * 
     * @param message
     * @return
     * @author w_jiqing
     */
    public CebExpaySuitNSQReq buildSuitNSQReqPlainMsg(CebExPayMessage message) {
        CebExpaySuitNSQReq suitNsq = new CebExpaySuitNSQReq();
        CebExpayNSQReq nsq = new CebExpayNSQReq();
        CebExPayMessageNSQReq nsqMes = new CebExPayMessageNSQReq();
        // 交易代码
        nsqMes.setTransId(cebConfig.get("CEB.EXPAY.NSQREQ.TRANSID"));
        // 商户号
        nsqMes.setMerId(message.getMerID());
        // 交易流水号
        nsqMes.setTrxId(cebConfig.get("CEB.EXPAY.NSQREQ.TRANSID") + message.getOrderId());
        // 银行卡号
        nsqMes.setCardNo(message.getCardNo());
        // 银行卡类型
        if (BisBankBusinessProd._05.value.equals(message.getBankBusProd().value)) {
            nsqMes.setCardType("C");
        } else {
            nsqMes.setCardType("D");
        }
        nsq.setId(this.getStringid());
        nsq.setPlain(nsqMes);
        suitNsq.setMessage(nsq);
        message.setTransId(nsqMes.getTransId());
        return suitNsq;

    }

    /**
     * 构建客户验证签名明文对象
     * 
     * @param message
     * @return
     */
    public MessageSuitUSCReq buildUscReqPlainMsg(CebExPayMessage message) {
        MessageSuitUSCReq suit = new MessageSuitUSCReq();
        MessageUSCReq msg = new MessageUSCReq();
        CebExPayMessageUSCReq uscReq = new CebExPayMessageUSCReq();
        BeanUtils.copyProperties(message, uscReq);
        uscReq.setVersion(cebConfig.get("CEB.EXPAY.VERSION"));
        uscReq.setTransId(cebConfig.get("CEB.EXPAY.USCREQ.TRANSID"));
        uscReq.setMerId(cebConfig.get("CEB.EXPAY.MERCHANTID"));
        uscReq.setPhone(message.getMediaId());
        if ("01".equals(message.getIdentityType())) {
            uscReq.setCertType("1");
        }
        uscReq.setCertNo(message.getIdentityCode());
        if (((BisBankBusinessProd._15.value).equals(message.getBankBusProd().value))
                || ((BisBankBusinessProd._20.value).equals(message.getBankBusProd().value))) {
            uscReq.setCardType("D");
        } else if (BisBankBusinessProd._05.value.equals(message.getBankBusProd().value)) {
            uscReq.setCardType("C");
            uscReq.setCvv2(message.getCvv2());
            uscReq.setValidDate(message.getAvailableDate());
        }

        uscReq.setName(message.getCardHolder());
        uscReq.setDate(CommonUtils.formatDate(new Date(), "yyyyMMddHHmmss"));
        uscReq.setBussType(cebConfig.get("CEB.EXPAY.BUSSTYPE"));

        msg.setId(this.getStringid());
        msg.setPlain(uscReq);
        suit.setMessage(msg);
        message.setTransId(uscReq.getTransId());
        return suit;
    }

    /**
     * 构建快捷支付请求报文
     * 
     * @param message
     * @return
     * @author w_jiqing
     */
    public CebExpaySuitPPReq buildPPReqPlainMsg(CebExPayMessage message) {
        CebExpaySuitPPReq suitPpreq = new CebExpaySuitPPReq();
        CebExpayPPReq ppReq = new CebExpayPPReq();
        CebExPayMessagePPReq ppReqMsg = new CebExPayMessagePPReq();
        CpsGenMainOrder mainOrder = mainOrderQuery.get(message.getOrderId());
        CustCorpInfo cci = custCorpInfoManager.get(mainOrder.getMainRecvCustId());
        BisBankContractInfo contractInfo = findContractInfo(message);
        // 交易代码
        ppReqMsg.setTransId(cebConfig.get("CEB.EXPAY.PPREQ.TRANSID"));

        // 商户号
        ppReqMsg.setMerId(message.getMerID());
        // 签约协议号
        ppReqMsg.setSignNo(contractInfo.getContractNumber());
        // 付款方名称
        ppReqMsg.setName(message.getCardHolder());
        // 证件类型 1-身份证
        ppReqMsg.setCertType("1");
        // 证件号码
        ppReqMsg.setCertNo(message.getIdentityCode());
        // 手机号码
        ppReqMsg.setPhone(message.getMediaId());
        // 交易流水号
        ppReqMsg.setSerialNo(message.getOrderId());
        // 交易日期和时间
        ppReqMsg.setDate(DateUtils.format(mainOrder.getCreatedTs(), DATE_FORMAT));
        // 银行卡号
        ppReqMsg.setCardNo(message.getCardNo());
        // 银行卡类型
        if (BisBankBusinessProd._05.value.equals(message.getBankBusProd().value)) {
            ppReqMsg.setCardType("C");
            ppReqMsg.setValidDate(message.getAvailableDate());
            ppReqMsg.setCvv2(message.getCvv2());
        } else {
            ppReqMsg.setCardType("D");
        }
        // 交易金额
        ppReqMsg.setAmount(message.getAmount());
        // 交易货币代码 默认为 156 人民币
        ppReqMsg.setCurrency("156");
        // 商品描述
        ppReqMsg.setGoods("快捷支付");
//        // 二级商户名称
//        ppReqMsg.setSubMerName("国付宝信息科技有限公司");
//        // 二级商户号
//        ppReqMsg.setMerSecId("0000000");
        // 商户名称
        ppReqMsg.setMrchntNm("国付宝信息科技有限公司");
        // 交易类型 1000-消费
        ppReqMsg.setTrxTP("1000");   
        //付款方账户所属机构标识
        ppReqMsg.setPyerAcctIssrId(cebConfig.get("CEB.EXPAY.PPREQ.PYERACCTISSRID"));
        // 收款方名称    收款方是与支付公司合作的下游商户
        if(cci == null){
            //因充值业务主收款客户id是空的，无法获取到，故cci为空，取默认值
            ppReqMsg.setPyeeNm("系统账号关联客户");
        }else{
            ppReqMsg.setPyeeNm(cci.getCorpName());
        }
        // 收款方账户
        ppReqMsg.setPyeeAcctId(mainOrder.getMainRecvAcct());
        // 收款方账户类型      0-借记
        ppReqMsg.setPyeeAcctTp("0");
        // 收款方账户所属机构标识
        ppReqMsg.setPyeeAcctIssrId(cebConfig.get("CEB.EXPAY.PPREQ.PYERACCTISSRID"));
        // 商户类型  1-法人
        ppReqMsg.setMrchntTp(cebConfig.get("CEB.EXPAY.PPREQ.MRCHNTTP"));
        // 商户证件类型     11-营业执照
        ppReqMsg.setMrchntCertTp(cebConfig.get("CEB.EXPAY.PPREQ.MRCHNTCERTTP"));
        // 商户证件编码
        ppReqMsg.setMrchntCertId(cebConfig.get("CEB.EXPAY.PPREQ.MRCHNTCERTID"));
        // 商户行业类别
        ppReqMsg.setMrchntCdgyCd(cebConfig.get("CEB.EXPAY.PPREQ.MRCHNTCDGYCD"));
        ppReq.setId(this.getStringid());
        ppReq.setPlain(ppReqMsg);
        suitPpreq.setMessage(ppReq);
        message.setTransId(ppReqMsg.getTransId());
        return suitPpreq;

    }

    /**
     * 构建快捷支付签名明文对象
     * 
     * @param message
     * @return
     */
    public MessageSuitQPReq buildQpReqPlainMsg(CebExPayMessage message) {
        MessageSuitQPReq suit = new MessageSuitQPReq();
        MessageQPReq msg = new MessageQPReq();
        CebExPayMessageQPReq req = new CebExPayMessageQPReq();
        req.setVersion(cebConfig.get("CEB.EXPAY.VERSION"));
        req.setTransId(cebConfig.get("CEB.EXPAY.QPREQ.TRANSID"));
        req.setMerId(cebConfig.get("CEB.EXPAY.MERCHANTID"));
        req.setPhone(message.getMediaId());
        req.setCertNo(message.getIdentityCode());
        req.setName(message.getCardHolder());
        req.setDate(message.getOrderDate());
        req.setSerialNo(message.getOrderId());
        req.setCardNo(message.getCardNo());
        req.setCurrency(cebConfig.get("CEB.EXPAY.CURRENCY"));

        if ("01".equals(message.getIdentityType())) {
            req.setCertType("1");
        }
        if (((BisBankBusinessProd._15.value).equals(message.getBankBusProd().value))
                || ((BisBankBusinessProd._20.value).equals(message.getBankBusProd().value))) {
            req.setCardType("D");
        } else if (BisBankBusinessProd._05.value.equals(message.getBankBusProd().value)) {
            req.setCardType("C");
            req.setCvv2(message.getCvv2());
            String availableDate = message.getAvailableDate();
            req.setValidDate(availableDate.substring(2, 4) + availableDate.substring(0, 2));
            req.setStageFlag("0");
        }
        req.setGoods("快捷支付");
        req.setSubMerName("国付宝信息科技有限公司");
        req.setBussType(cebConfig.get("CEB.EXPAY.BUSSTYPE"));
        req.setAmount(message.getAmount().toString());

        msg.setId(this.getStringid());
        msg.setPlain(req);
        suit.setMessage(msg);
        message.setTransId(req.getTransId());
        return suit;
    }

    /**
     * 构建单笔查询签名明文对象
     * 
     * @param message
     * @return
     */
    public MessageSuitSOQReq buildSoqReqPlainMsg(CebExPayMessage message) {
        MessageSuitSOQReq suit = new MessageSuitSOQReq();
        MessageSOQReq msg = new MessageSOQReq();
        CebExPayMessageSOQReq req = new CebExPayMessageSOQReq();
        req.setVersion(cebConfig.get("CEB.EXPAY.VERSION"));
        req.setTransId(cebConfig.get("CEB.EXPAY.SOQREQ.TRANSID"));
        req.setMerId(cebConfig.get("CEB.EXPAY.MERCHANTID"));
        req.setSerialNo(message.getOrderId());
        req.setTransType(cebConfig.get("CEB.EXPAY.QUERY.TYPE"));
        req.setDate(CommonUtils.formatDate(new Date(), "yyyyMMddHHmmss"));
        msg.setId(this.getStringid());
        msg.setPlain(req);
        suit.setMessage(msg);
        message.setTransId(req.getTransId());

        return suit;
    }

    /**
     * 构建订单查询报文
     * 
     * @param message
     * @return
     * @author w_jiqing
     */
    public CebExpaySuitNSOQReq buildNSOQPlainMsg(CebExPayMessage message) {
        CebExpaySuitNSOQReq suit = new CebExpaySuitNSOQReq();
        CebExpayNSOQReq nsoq = new CebExpayNSOQReq();
        CebExPayMessageNSOQReq nsoqMsg = new CebExPayMessageNSOQReq();
        CpsGenMainOrder mainOrder = mainOrderQuery.get(message.getOrderId());
        // 版本号
        nsoqMsg.setVersion(cebConfig.get("CEB.EXPAY.NSOQREQ.VERSION"));
        // 交易代码
        nsoqMsg.setTransId(cebConfig.get("CEB.EXPAY.NSOQReq.TRANSID"));
        // 交易流水号
        nsoqMsg.setTrxId(cebConfig.get("CEB.EXPAY.NSOQReq.TRANSID") + message.getOrderId());
        // 商户号
        nsoqMsg.setMerId(message.getMerID());
        // 订单交易日期和时间
        nsoqMsg.setDate(DateUtils.format(mainOrder.getCreatedTs(), DATE_FORMAT));
        // 交易性质 1-支付
        nsoqMsg.setType("1");
        // 订单流水号
        nsoqMsg.setSerialNo(message.getOrderId());
        nsoq.setId(this.getStringid());
        nsoq.setPlain(nsoqMsg);
        suit.setMessage(nsoq);
        message.setTransId(nsoqMsg.getTransId());
        return suit;

    }
    
    /**
     * 根据卡号和签约银行查询生效签约记录
     * 
     * @param message
     * @return
     */
    public BisBankContractInfo findContractInfo(ExPayBaseMessage message) {
        BisBankContractInfo bisBankContractInfo = new BisBankContractInfo();
        bisBankContractInfo.setBankCode(message.getBank());
        bisBankContractInfo.setCardNo(message.getCardNo());
        bisBankContractInfo.setBankBizProdCode(message.getBankBusProd().value);
        bisBankContractInfo.setIntrfaceChannelCode(message.getBankIntrChann().value);
        return bankContractInfoDAO.getContractInfoByCard(bisBankContractInfo);
    }

    public String getStringid() {
        long time = System.currentTimeMillis();
        String id = String.valueOf(time);
        return id;
    }

    public void setCebExPayServiceClient(ExPayService cebExPayServiceClient) {
        this.cebExPayServiceClient = cebExPayServiceClient;
    }

    public void setCebConfig(Map<String, String> cebConfig) {
        this.cebConfig = cebConfig;
    }

}